package com.oa.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.oa.back.ResponseBack;
import com.oa.common.dto.FormDto;
import com.oa.common.enums.AppsType;
import com.oa.common.enums.CodeStatus;
import com.oa.common.enums.apps.FromFileType;
import com.oa.common.utils.RandomUnit;
import com.oa.common.vo.FormVo;
import com.oa.dao.BaseDao;
import com.oa.dao.FormDao;
import com.oa.entities.User;
import com.oa.entities.flows.AppType;
import com.oa.entities.flows.Form;
import com.oa.service.AppsTypeService;
import com.oa.service.FormService;
import com.oa.service.UndeleteServiceImpl;
import com.oa.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class FormServiceImpl extends UndeleteServiceImpl<Long,Form> implements FormService {

    private static final Logger logger= LoggerFactory.getLogger(FormServiceImpl.class);

    @Autowired
    private FormDao dao;

    @Autowired
    private UserService userService;

    @Autowired
    private AppsTypeService appsTypeService;

    @Override
    protected BaseDao<Long, Form> getSelfDao() {
        return dao;
    }

    @Override
    public void setClass(Class<Form> clazz) {

    }

    @Override
    @Transactional
    public ResponseBack<FormVo> submit(String params) {
        if (StringUtils.isEmpty(params)){
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        FormDto dto= JSONObject.parseObject(params,FormDto.class);
        if(dto==null){
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        AppType appsType= null;
        if (dto.getTypeId()==null || (appsType= appsTypeService.get(dto.getTypeId()))==null){
            return ResponseBack.fial(CodeStatus.APPSTYPE_UNDERFUND.getResponseStatus());
        }
        logger.warn("输出的数据传入类:"+dto);
        Form form= get(dto.getId());
        if (form==null){
            form= new Form();
        }
        form.setName(dto.getName());
        form.setAppType(appsType);
        form.setConfiraction(dto.getConfiraction());
        form.setDbType(dto.getDbType());
        form.setFileType(dto.getFileType());
        form.setH(dto.getH());
        /**
         * 当没有表单时，
         * 直接创建新的表单
         */
        if (form.getId()==null){
            String inputName="toa_"+RandomUnit.getRandom();
            inputName+=String.valueOf(System.currentTimeMillis());
            form.setInputName(inputName);
        }
        form.setInputValue(dto.getInputValue());
        form.setMagnificent(dto.getConfiraction());
        form.setTypeId(dto.getTypeId());
        form.setW(dto.getW());
        if (dto.getOptions()!=null && dto.getOptions().length>0){
            form.setOptiones(Arrays.asList(dto.getOptions()));
        }
        User user= userService.getCurrent();
        logger.warn("当前登录的用户:"+user);
        form.setIssue(user);
        form.setIssueId(user.getId());
        if (form.getId()==null){
            dao.alterTable(form);
            save(form);
        }else {
            update(form);
        }
        FormVo vo= new FormVo(form);
        logger.warn("提交返回类的数据"+vo);
        return ResponseBack.success(vo);
    }

    @Override
    public List<Form> findForm(Long typeId) {
        return dao.findForm(typeId);
    }

    @Override
    public Form find(Long typeId, String name) {
        return dao.find(typeId, name);
    }

    @Override
    public List<FormVo> find(Long typeId) {
        List<Form> forms= dao.findForm(typeId);
        List<FormVo> vos= new ArrayList<>();
        if (forms!=null && forms.size()>0){
            for (Form form: forms){
                vos.add(new FormVo(form));
            }
        }
        return vos;
    }
}
